package com.javaee.thinks.tools;

import com.javaee.thinks.po.Information;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Notebook {
    //增
    public static void rememberInfo(String infoSetName,String infoText){
        try {
            SqlSessionFactory sqlSessionFactory;
            Reader reader = Resources.getResourceAsReader("config/mybatis_config.xml"); // 替换为你的配置文件路径
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            Information information=new Information("test", "testing");
            sqlSession.insert("com.javaee.thinks.po.Information.rememberInfo",information);
            sqlSession.commit();
            sqlSession.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
    //删
    public static void deleteByInfoSetName(String infoSetName){
        try {
            SqlSessionFactory sqlSessionFactory;
            Reader reader = Resources.getResourceAsReader("config/mybatis_config.xml"); // 替换为你的配置文件路径
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            sqlSession.delete("com.javaee.thinks.po.Information.deleteByInfoSetName", infoSetName);
            sqlSession.commit();
            sqlSession.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
    //改
    public static void updateInfoText(String infoSetName,String oldInfoText,String newInfoText){
        try {
            SqlSessionFactory sqlSessionFactory;
            Reader reader = Resources.getResourceAsReader("config/mybatis_config.xml"); // 替换为你的配置文件路径
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            Map map=new HashMap();
            map.put("infoSetName",infoSetName);
            map.put("oldInfoText",oldInfoText);
            map.put("newInfoText",newInfoText);
            sqlSession.update("com.javaee.thinks.po.Information.updateInfoText",map);
            sqlSession.commit();
            sqlSession.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
    //查
    public static String showInfoByName(String infoSetName){
        try {
            SqlSessionFactory sqlSessionFactory;
            Reader reader = Resources.getResourceAsReader("config/mybatis_config.xml"); // 替换为你的配置文件路径
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            List<Information> informations = sqlSession.selectList("com.javaee.thinks.po.Information.showInfoByName", infoSetName);
            sqlSession.commit();
            sqlSession.close();
            String  result="任务("+informations.get(0).getInfoSetName()+")已搜集到的信息：";
            for (Information information : informations) {
                result+=information.getInfoText()+";";
            }
            return result;
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
